// void, Obj This
Settlement this, sett_target;
Building bld;
int min_resource_food, min_resource_gold, minpop, i, j;
int nSendGold, nSendFood, p;
Wagon wagon;
ObjList olBuildings, olUnitsTributeOut, sel;
bool bBrokenVillage = false;
bool aicheck;
str target_name;
Sleep(rand(1000)+500);
this = This.AsBuilding().settlement;
bld = This.AsBuilding();

if(EnvReadInt(this, "AssignName") == 0){
	Building build;
	str names;
	
	EnvWriteInt(this, "AssignName", 1);
	if(bld.name == ""){
		names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
		build = GetNamedObj(names).obj.AsBuilding;
		if(!build.IsValid){
			bld.SetName("");       
			bld.SetName(names);
		} else {
			names = "B_"+bld.class+"_"+bld.player+bld.race+rand(99)+rand(99);
			bld.SetName("");       
			bld.SetName(names);
		}  
	}
}

p = .player;
if(EnvReadInt(this, "player") != p){
	EnvWriteString(this, "tribute_food", "off");
	EnvWriteString(this, "tribute_gold", "off");
	EnvWriteString(this, "tribute_people", "off");
	                                
	EnvWriteString(this, "stoptribute_food", "on");
	EnvWriteString(this, "stoptribute_gold", "on");
	EnvWriteString(this, "stoptribute_people", "on");
	.StopSupply();
}
EnvWriteInt(this, "player", p);
               
min_resource_food = GetConst("MinResQtyToTransportFood");
min_resource_gold = GetConst("MinResQtyToTransportGold");
minpop = GetConst("MinPopulation");

//r = EnvReadInt(.player, "RACE");
if(EnvReadInt(this, "onepop") == 0){
	if (bld.race==Germany){
		if(.max_population < 80)
			.AddToMaxPopulation(10);
		if(.population < 60)	
			.SetPopulation(.population + 30);	
	}
	EnvWriteInt(this, "onepop", 1);
}

while (1)
{
	j+=1;
	if(j >= 10){
		j = 0;
		for (i = 0; i < 8; i += 1){
			Sleep(100);
			if(EnvReadString(this, "Village map check") != "yes"){
				if(EnvReadString(i, "Village map") == "yes"){
					Obj WatchEye;
					Sleep(rand(300)+1000);
					WatchEye = Place("WatchEye", This.AsBuilding().pos, i);
					WatchEye.SetSight(800);
					WatchEye.SetCommand("infinite_spy");
					
					EnvWriteString(this, "Village map check", "yes");
				}
			}
		}
	}
	
	if(.max_population < 100){
		int cost_food;
		
		if(.max_population >= 1 && .max_population < 40){
			cost_food = 700;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 40 && .max_population < 50){
			cost_food = 1000;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 50 && .max_population < 60){
			cost_food = 1300;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 60 && .max_population < 70){
			cost_food = 1600;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 70 && .max_population < 80){
			cost_food = 1900;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 80 && .max_population < 90){
			cost_food = 2200;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		} else if(.max_population >= 90 && .max_population < 100){
			cost_food = 2500;
			if(.food >= cost_food){
				.AddToMaxPopulation(5);
				.SetFood(.food - cost_food);
			}
		}              
    }
	
	if(.population >= .max_population){
		if(EnvReadString(this, "auto_call_peasants") == "yes"){
			point ptDummy; Obj objDummy;
			str strCmd;

			strCmd = "trainpeasant"+bld.raceStrPrefLow; 
			bld.ExecCmd(strCmd, ptDummy, objDummy, false);
		}
	}
		
	aicheck = true;
	Sleep(1000);
	if(p != .player){
		EnvWriteString(this, "tribute_food", "off");
		EnvWriteString(this, "tribute_gold", "off");
		EnvWriteString(this, "tribute_people", "off");
								
		EnvWriteString(this, "stoptribute_food", "on");
		EnvWriteString(this, "stoptribute_gold", "on");
		EnvWriteString(this, "stoptribute_people", "on");
		.StopSupply();
		break;
	}
	if (.supplied == this)
		.StopSupply();
	if (!.supplied.IsValid()) aicheck = false;
        
	if (.IsEnemy(.supplied.player)) aicheck = false;
	//pr("village");
	
	bBrokenVillage = false;
	if (.population < minpop)
	{	// hold 1000 gold to can repear
		Building b;
		
		bBrokenVillage = true;
		if (!.GetCentralBuilding().IsBroken())
		if (!.GetCentralBuilding().IsVeryBroken())
			bBrokenVillage = false;
		olBuildings = .Buildings();
		for (i = 0; i < olBuildings.count;i += 1)
		{
			b = olBuildings[i];
			if (!b.IsValid) continue;
			if ((b.IsBroken() || b.IsVeryBroken())) {
				bBrokenVillage = true;
				break;
			}
		}
	}
	nSendGold = .gold;
	nSendFood = .food;
	if (bBrokenVillage) {
		int gold, food;
		if (GetCmdCost("repair village", gold, food)) {
			nSendGold -= gold;
			nSendFood -= food;
		}
	}

	if(EnvReadString(this, "tribute_gold") == "on"){
					
		target_name = EnvReadString(this, "tribute_gold_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		
		if(sett_target.IsValid){
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_gold", "off");
				EnvWriteString(this, "stoptribute_gold", "on");
				continue;
			}
			if(nSendGold > min_resource_gold){
				wagon = .CreateMuleGold(MIN(1000, nSendGold));
				if(wagon.IsValid){
					wagon.SetCommand("unload", sett_target.GetCentralBuilding());
					wagon.AddCommand(false, "unload", .GetCentralBuilding()); // return back is somthing happens
					continue;
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("BaseVillage");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						UserNotification("tribute gold", "", sett_target.GetCentralBuilding.pos, .player); 
						//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
					}
				}
			}
		}
	}
	Sleep(1000);

	if(EnvReadString(this, "tribute_food") == "on" || (IsAIPlayer(.player) && aicheck)){
		
		target_name = EnvReadString(this, "tribute_food_target");
		sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;
		
		if(sett_target.IsValid || (IsAIPlayer(.player) && aicheck)){
			if(!IsAIPlayer(.player))
			if(.IsEnemy(sett_target.player)) {
				EnvWriteString(this, "tribute_food", "off");
				EnvWriteString(this, "stoptribute_food", "on");
				.StopSupply();
				continue;
			}
			if(nSendFood > min_resource_food){
				if(IsAIPlayer(.player) && aicheck){
					wagon = .CreateMuleFood(MIN(1000, nSendFood));
					if(wagon.IsValid){
						wagon.SetCommand("unload", .supplied.GetCentralBuilding());
						wagon.AddCommand(false, "unload", .GetCentralBuilding()); // return back is somthing happens
						continue;
					}
				 
				} else {
					wagon = .CreateMuleFood(MIN(1000, nSendFood));
					if(wagon.IsValid){
						wagon.SetCommand("unload", sett_target.GetCentralBuilding());
						wagon.AddCommand(false, "unload", .GetCentralBuilding()); // return back is somthing happens
						continue;
					}
				}
			}
			sel = _GetSelection().ObjAlly(.player).ObjClass("BaseVillage");
			if(sel.count == 1){
				Building tw;
				
				tw = sel[0].AsBuilding;
				if(tw.IsValid){
					if(tw == .GetCentralBuilding){
						//UserNotification("tribute people", "", Point(target_x, target_y), .player); 
						//UserNotification("tribute gold", "", sett_target.GetCentralBuilding.pos, .player); 
						UserNotification("tribute food", "", sett_target.GetCentralBuilding.pos, .player); 
					}
				}
			}
		}
	}
	Sleep(1000);
	if(EnvReadString(this, "tribute_people") == "on"){
		olUnitsTributeOut.Clear;
     
		for(i=0; i<.Units.count; i+=1) {
			if(.Units[i].IsValid){
				if(!.Units[i].AsUnit.HasItem("Veteran Offence")){
					.Units[i].AsUnit.SetFeeding( true );  
					olUnitsTributeOut.Add(.Units[i]);
				}
			}
		}
		
		{
			int target_x, target_y;
			point pos, pp;
			rect rc;
			
			target_name = EnvReadString(this, "tribute_people_target");
			sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;                        
			target_x = EnvReadInt(this, "tribute_people_target_x");
			target_y = EnvReadInt(this, "tribute_people_target_y");
			if(sett_target.IsValid){
				Building bld_target;
				bld_target = sett_target.GetCentralBuilding;
				if(!.IsEnemy(sett_target.player)){
					if(olUnitsTributeOut.count >= 1){
						if(bld_target.IsHeirOf("Outpost") ||
						   bld_target.IsHeirOf("BaseShipyard") ||
						   bld_target.IsHeirOf("IronMine") ||
						   bld_target.IsHeirOf("BaseVillage") ||
						   bld_target.IsHeirOf("BaseTownhall")){
							
							if(bld_target.IsHeirOf("BaseVillage")){
								for(i=0; i<olUnitsTributeOut.count; i+=1) {
									if(olUnitsTributeOut[i].IsValid){
										if(olUnitsTributeOut[i].AsUnit.IsMilitary)
											olUnitsTributeOut[i].SetCommand("move", bld_target.pos);
										else
											olUnitsTributeOut[i].SetCommand("enter", bld_target);
									}
								}
							} else {
								olUnitsTributeOut.SetCommand("enter", bld_target);
							}
								
						} else {
							rc = GetMapRect();
							pos.Set(0, bld_target.sight / 3);
							pos.Rot(rand(360));     pp = pos + bld_target.pos; pp.IntoRect(rc); 
							olUnitsTributeOut.SetCommand("move", pp);
						}
						//Sleep(4*1000);
					}
				} else {
					EnvWriteString(this, "tribute_people", "off");
					EnvWriteString(this, "stoptribute_people", "on");
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("BaseVillage");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", bld_target.pos, .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			} else if(target_x != 0 && target_y != 0){
				if(olUnitsTributeOut.count >= 1){
					rc = GetMapRect();
					pos.Set(0, 200);
					pos.Rot(rand(360));     pp = pos + Point(target_x, target_y); pp.IntoRect(rc); 
					olUnitsTributeOut.SetCommand("move", pp);

					//Sleep(4*1000);
				}
				sel = _GetSelection().ObjAlly(.player).ObjClass("BaseVillage");
				if(sel.count == 1){
					Building tw;
					
					tw = sel[0].AsBuilding;
					if(tw.IsValid){
						if(tw == .GetCentralBuilding){
							UserNotification("tribute people", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute gold", "", Point(target_x, target_y), .player); 
							//UserNotification("tribute food", "", Point(target_x, target_y), .player); 
						}
					}
				}
			}
		}
	}
}
